package com.sunseagear.wind.modules.sms.controller;

import com.google.common.collect.Maps;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.utils.JsonUtils;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.sms.dto.MsgSendDto;
import com.sunseagear.wind.modules.sms.service.ISmsSendService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version V1.0
 * @title 短信发送
 * @description 短信发送
 * @date 2017-06-08 12:56:37
 */
@RestController
@RequestMapping("/sms/send")
@Log(title = "短信发送")
@RequiredArgsConstructor
public class SmsSendController {
    private final ISmsSendService smsSendService;

    /**
     * 发送短信
     *
     * @param dto 参数对象
     * @return 发送结果
     */
    @PostMapping(value = "/sendSmsByCode")
    @Log(logType = LogType.OTHER, title = "短信发送")
    public R<Void> sendSmsByCode(@RequestBody MsgSendDto dto) {
        try {
            String[] phones = dto.getPhone().split(",");
            if (!StringUtils.isEmpty(dto.getData())) {
                smsSendService.send(phones, dto.getCode(), JsonUtils.toMap(StringEscapeUtils.unescapeHtml4(dto.getData())));
            } else {
                smsSendService.send(phones, dto.getCode(), Maps.newHashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("短信发送失败");
        }
        return R.success("短信发送成功");
    }

    /**
     * 群发短信
     *
     * @return
     */
    @PostMapping(value = "/massSendSmsByCode")
    public R<Void> massSendSmsByCode(@RequestBody MsgSendDto dto) {
        try {

            String[] phones = dto.getPhone().split(",");
            if (!StringUtils.isEmpty(dto.getData())) {
                smsSendService.send(phones, dto.getCode(), JsonUtils.toMap(StringEscapeUtils.unescapeHtml4(dto.getData())));
            } else {
                smsSendService.send(phones, dto.getCode(), Maps.newHashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("短信发送失败");
        }
        return R.success("短信发送成功");
    }
}
